<template>
  <div class="login-container">
    <div class="project-name">{{ $ts(title) }}</div>
    <div class="login-form">
      <el-tabs
        v-model="activeName"
        class="demo-tabs"
        @tab-click="handleClick"
      >
        <!-- <el-tab-pane
          label="Okta Login"
          name="first"
        >
          <div
            class="form-user"
            @keypress="loginPress"
          >
            <div class="login-text">
              <div>
                <div>{{ $ts("欢迎登录") }}...</div>
                <div class="login-line"></div>
              </div>
              <div style="flex: 1"></div>
            </div>
            <div class="login-text-small">WELCOME TO LOGIN</div>
          </div>
          <div class="loging-btn">
            <el-button
              size="large"
              :loading="loading"
              color="#3a6cd1"
              :dark="true"
              @click="login"
              long
            >
              <span v-if="!loading">Okta Login</span>
              <span v-else>Okta Logining...</span>
            </el-button>
          </div>
        </el-tab-pane> -->
        <el-tab-pane
          label="UserName Login"
          name="second"
        >
          <div
            class="form-user"
            @keypress="loginPress"
          >
            <div class="login-text">
              <div>
                <div>{{ $ts("欢迎登录") }}...</div>
                <div class="login-line"></div>
              </div>
              <div style="flex: 1"></div>
            </div>
            <div class="login-text-small">WELCOME TO LOGIN</div>
            <div class="item">
              <div class="input-icon el-icon-user"></div>
              <input
                type="text"
                v-focus
                v-model="userInfo.userName"
                placeholder="请输入账号"
              />
            </div>
            <div class="item">
              <div class="input-icon el-icon-lock"></div>
              <input
                type="password"
                v-focus
                v-model="userInfo.password"
                placeholder="请输入密码"
              />
            </div>
            <div class="loging-btn">
              <el-button
                size="large"
                :loading="loading"
                color="#3a6cd1"
                :dark="true"
                @click="UserNamelogin"
                long
              >
                <span v-if="!loading">Login</span>
                <span v-else>Logining...</span>
              </el-button>
            </div>
          </div>

        </el-tab-pane>
      </el-tabs>
    </div>
    <img
      class="login-bg"
      src="/static/login_bg.png"
    />
  </div>
</template>

<script>
import {
  defineComponent,
  ref,
  reactive,
  getCurrentInstance,
  onMounted,
} from "vue";
import BusConfig from "../../ProConfig";
import { useRouter } from "vue-router";
import store from "../store/index";
import http from "@/../src/api/http.js";
import api from "../api/api";
export default defineComponent({
  setup(props, context) {
    store.commit("clearUserInfo", "");
    const loading = ref(false);
    const codeImgSrc = ref("");
    const userInfo = reactive({
      userName: "",
      password: "",
      verificationCode: "======",
      UUID: "======",
    });
    onMounted(() => {
      if (process.env.NODE_ENV === "production") {
          apiUrl.value = process.env.VUE_APP_API_URL_PROD;
      } else if (process.env.NODE_ENV === "staging") {
          apiUrl.value = process.env.VUE_APP_API_URL_STG;
      } else {
          apiUrl.value = process.env.VUE_APP_API_URL_DEV;
      }
    });
    const activeName = ref("second");
    const apiUrl = ref(null);
    const getVierificationCode = () => {
      http.get("/api/User/getVierificationCode").then((x) => {
        codeImgSrc.value = "data:image/png;base64," + x.img;
        userInfo.UUID = x.uuid;
      });
    };
    let appContext = getCurrentInstance().appContext;
    let $message = appContext.config.globalProperties.$message;
    let router = useRouter();
    let Cookie = ref({});
    let title = ref(BusConfig.title);
    const UserNamelogin = async () => {
      if (!userInfo.userName) return $message.error("请输入用户名");
      if (!userInfo.password) return $message.error("请输入密码");
      let result = await api.AccountLogin(userInfo);
      if (result.status) {
        let obj = {
          token: result.data.token,
          img: "",
          userName: "",
          NumberWork: "",
          userTrueName: "",
        };
        store.commit("setUserInfo", obj);
        let res = await api.GetCurrentUserInfo();
        if (res.status) {
          obj.img = res.data.headImageUrl;
          obj.userName = res.data.userTrueName;
          obj.NumberWork = res.data.userName;
          obj.userTrueName = res.data.userTrueName;
        }
        store.commit("setUserInfo", obj);
        store.getters.data().ischeinese = true;
        localStorage.setItem("vol3_theme", "dark");
        router.push({ path: "/" });
      } else {
        $message.error(result.message);
      }
    };

    const login = async () => {
      Cookie.value = getCookie("jwtToken");
      if (
        Cookie.value === undefined ||
        Cookie.value === "" ||
        Cookie.value === null
      ) {
        window.location.href = apiUrl.value;
      } else {
        $message.success("Login successful, rebounding....");
        let obj = {
          token: Cookie.value,
          img: "",
          userName: "",
          NumberWork: "",
          userTrueName: "",
        };
        store.commit("setUserInfo", obj);
        let res = await api.GetCurrentUserInfo();
        if (res.status) {
          obj.img = res.data.headImageUrl;
          obj.userName = res.data.userTrueName;
          obj.NumberWork = res.data.userName;
          obj.userTrueName = res.data.userTrueName;
        }
        store.commit("setUserInfo", obj);
        // console.log("已经登录过了",obj)
        store.getters.data().ischeinese = true;
        localStorage.setItem("vol3_theme", "dark");
        router.push({ path: "/" });
      }
    };
    const loginPress = (e) => {
      if (e.keyCode == 13) {
        login();
      }
    };
    const getCookie = (name) => {
      const value = `; ${document.cookie}`;
      const parts = value.split(`; ${name}=`);
      if (parts.length === 2) return parts.pop().split(";").shift();
    };
    const openUrl = (url) => {
      window.open(url, "_blank");
    };
    return {
      UserNamelogin,
      activeName,
      title,
      getCookie,
      Cookie,
      loading,
      codeImgSrc,
      getVierificationCode,
      login,
      userInfo,
      loginPress,
      openUrl,
    };
  },
  mounted() {
    if (this.$route.fullPath == "/login") {
      this.login();
    }
    console.log("Current route:", this.$route.fullPath);
    // this.login();
  },
  directives: {
    focus: {
      inserted: function (el) {
        el.focus();
      },
    },
  },
});
</script>
<style lang="less" scoped>
.login-container {
  display: flex;
  width: 100%;
  height: 100%;
  background: rgb(246, 247, 252);
  justify-content: flex-end;
  align-items: center;
}

.login-form {
  align-items: center;
  width: 50%;
  display: flex;
  flex-direction: column;
  z-index: 999;

  .form-user {
    .item {
      border-radius: 5px;
      border: 1px solid #ececec;
      display: flex;
      margin-bottom: 30px;
      background: #ffff;
      height: 45px;
      padding-left: 20px;

      .code {
        position: relative;
        cursor: pointer;
        width: 74px;
        padding: 5px 10px 0 0;
      }

      .input-icon {
        line-height: 45px;
        color: #7a7a7a;
        padding-right: 20px;
      }
    }
  }

  input:-webkit-autofill {
    box-shadow: 0 0 0px 1000px white inset;
    -webkit-box-shadow: 0 0 0px 1000px white inset !important;
  }

  input {
    background: white;
    display: block;
    box-sizing: border-box;
    width: 100%;
    min-width: 0;
    margin: 0;
    padding: 0;
    color: #323233;
    line-height: inherit;
    text-align: left;
    border: 0;
    outline: none;
    font-size: 16px;
  }
}

.form-user,
.loging-btn {
  width: 400px;
}

.loging-btn {
  box-shadow: 2px 4px 11px #a4c2ff;
  margin-top: 10px;

  span {
    font-size: 24px !important;
  }

  button {
    padding: 21px;
    font-size: 14px !important;
    width: 100%;
  }
}

.login-text {
  font-weight: bolder;
  font-size: 20px;
  letter-spacing: 2px;

  position: relative;
  display: flex;

  .login-line {
    z-index: -1;
    padding: 5px;
    position: relative;
    top: -8px;
    width: 100%;
    background-image: linear-gradient(to right, #6598ff, white);
  }
}

.login-text-small {
  margin-bottom: 20px;
  font-size: 13px;
  color: #7d7c7c;
}

.login-bg {
  left: 0;
  position: absolute;
  height: 100%;
  width: 50%;
  z-index: 0;
}

.project-name {
  position: absolute;
  top: 40px;
  left: 40px;
  z-index: 9999;
  font-weight: bolder;
  background-image: linear-gradient(to right, #1850c1, #9c009c);
  -webkit-background-clip: text;
  color: transparent;
  font-size: 25px;
}
</style>
<style lang="less" scoped>
.app-link {
  text-align: center;
  padding-top: 5px;
  font-size: 12px;
  width: 400px;
  padding-left: 40px;
  display: flex;

  a {
    flex: 1;
    position: relative;
    cursor: pointer;
    width: 70px;
    color: #666666;
    margin: 2px 10px 0 0;
  }

  img {
    display: none;
  }

  a:hover {
    color: #0545f6 !important;

    img {
      display: block;
      position: absolute;
      z-index: 999999999;
      top: -130px;
      width: 120px;
      left: -22px;

      border: 1px solid #b1b1b1;
    }
  }
}

.login-footer {
  position: absolute;
  width: 50%;
  bottom: 0.5rem;
  font-size: 12px;
  text-align: center;
  padding-bottom: 10px;
  color: #4f4f4f;

  a {
    margin-right: 10px;
    font-size: 12px;
    color: #4f4f4f;
  }

  div {
    margin-bottom: 5px;
  }

  a:hover {
    cursor: pointer;
    color: #0540e3 !important;
  }
}

.account-info {
  font-size: 12px;
  color: #636363;
  margin-top: 15px;
}
</style>

<style lang="less" scoped>
@media screen and (max-width: 700px) {
  .login-bg,
  .account-info,
  .app-link,
  .login-footer,
  .project-name {
    display: none;
  }

  .login-container {
    padding: 2rem;
    justify-content: center;
  }

  .login-form {
    width: 100%;
  }

  .form-user,
  .loging-btn {
    width: 100%;
  }
}
</style>
